//
//  JJChatBarVoiceView.swift
//  swiftProject
//
//  Created by yangjian on 2021/12/12.
//

import UIKit
import RxSwift

class JJChatBarVoiceView: UIView {
    let bag = DisposeBag()
    let button = UIButton()
//    let label = UILabel()
    
    let directoryPath : String = {
        return directory_voice(appkey: EMClient.shared().options.appkey)
    }()

    
    let rx_recordVoiceFinished = PublishSubject<(String,Int)>()
    
    var recording = false
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.config()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func config(){
        self.addSubview(button)
        self.button.snp.makeConstraints { make in
            make.center.equalTo(self)
            make.size.equalTo(CGSizeMake(100, 50))
        }
        
        self.button.setTitle("按下发言", for: .normal)
        self.button.setTitle("松开发送", for: .highlighted)
        
        self.button.insetsLayoutMarginsFromSafeArea = false
        self.button.backgroundColor = UIColor.orange
        self.button.rx.controlEvent(.touchDown).subscribe(onNext: {[unowned self] sender in
            print("touchDown-\(self.button.isHighlighted)-\(self.button.isSelected)")
            self.startRecord()
        }).disposed(by: bag)
        self.button.rx.controlEvent(.touchDragEnter).subscribe(onNext: {[unowned self] sender in
            print("touchDragEnter-\(self.button.isHighlighted)-\(self.button.isSelected)")
        }).disposed(by: bag)
        self.button.rx.controlEvent(.touchDragExit).subscribe(onNext: {[unowned self] sender in
            self.button.setTitle("取消发言", for: .normal)
            print("touchDragExit-\(self.button.isHighlighted)-\(self.button.isSelected)")
        }).disposed(by: bag)
        self.button.rx.controlEvent(.touchUpInside).subscribe(onNext: {[unowned self] sender in
            self.button.setTitle("按下发言", for: .normal)
            self.stopRecordAndSend()
            print("touchUpInside-\(self.button.isHighlighted)-\(self.button.isSelected)")
        }).disposed(by: bag)
        self.button.rx.controlEvent(.touchUpOutside).subscribe(onNext: {[unowned self] sender in
            self.button.setTitle("按下发言", for: .normal)
            self.cancelRecord()
            print("touchUpOutside-\(self.button.isHighlighted)-\(self.button.isSelected)")
        }).disposed(by: bag)
        self.button.rx.controlEvent(.touchCancel).subscribe(onNext: {[unowned self] sender in
            self.button.setTitle("按下发言", for: .normal)
            self.cancelRecord()
            print("touchCancel-\(self.button.isHighlighted)-\(self.button.isSelected)")
        }).disposed(by: bag)
    }
    func startRecord(){
        print("开始录制")
        
        if recording {
            return
        }
        recording = true
        JJVoiceRecordHelper.shared().startRecord_path("\(directoryPath)/\(NSDate().timeIntervalSince1970 * 1000)") { error in
            if error != nil {
                print("录制出现问题,错误")
                return
            }
        }
    }
    func stopRecordAndSend(){
        self.recording = false
        JJVoiceRecordHelper.shared().stopRecord_completion {[unowned self] amrPath, duration in
            rx_recordVoiceFinished.onNext((amrPath,duration))
        }
        print("结束录制")
    }
    func cancelRecord(){
        self.recording = false
        JJVoiceRecordHelper.shared().cancelRecord()
        print("取消录制")
    }
}
